package logic

import (
	"context"
	"house/models/mysql"

	"house/bookingrpc/bookingrpc"
	"house/bookingrpc/internal/svc"

	"github.com/zeromicro/go-zero/core/logx"
)

type BookingListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBookingListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BookingListLogic {
	return &BookingListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *BookingListLogic) BookingList(in *bookingrpc.BookingListRequest) (*bookingrpc.BookingListResponse, error) {
	// todo: add your logic here and delete this line
	var bookings []mysql.BookIng
	mysql.DB.Debug().Find(&bookings)
	tx := mysql.DB.Debug().Where("user_id=?", in.UserId)
	if in.BookingTimeOrder == 2 {
		tx.Order("book_ings_time asc")
	} else {
		tx.Order("book_ings_time desc")
	}
	tx.Find(&bookings)

	var BookingList []*bookingrpc.BookingList
	for _, booking := range bookings {
		list := bookingrpc.BookingList{
			Address: booking.Address,
			Types:   booking.Types,
			Time:    booking.Time,
			Contact: booking.Contact,
			HouseId: int64(booking.HouseId),
			UserId:  int64(booking.UserId),
		}
		BookingList = append(BookingList, &list)
	}
	return &bookingrpc.BookingListResponse{}, nil
}
